package com.wzy.orderSystem.service.impl;

import com.wzy.orderSystem.dao.FoodDao;
import com.wzy.orderSystem.pojo.is_role;
import com.wzy.orderSystem.service.opIsRole;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class opIsRoleImpl implements opIsRole {
    @Override
    public List<is_role> selectById(int id) throws SQLException {
        ArrayList<is_role> is_roles = new ArrayList<>();
        ResultSet resultSet = null ;
        try {
            String sql = "SELECT * FROM `is_role` WHERE id = ?";
            Object[] obj = {id};
            resultSet = FoodDao.selectByArgs(sql, obj);
            while(resultSet.next()){
                int roleId = resultSet.getInt(1);
                String roleName = resultSet.getString(2);

                is_role is_role = new is_role();
                is_role.setId(roleId);
                is_role.setRoleName(roleName);

                is_roles.add(is_role);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            resultSet.close();
        }
        return is_roles;
    }

    @Override
    public int updateById(int id, String roleName) {
        int rows = 0;
        String sql = "UPDATE `is_role` SET roleName=? WHERE id=?";
        Object[] obj ={roleName,id};
       rows = FoodDao.updateByArgs(sql, obj);
        return rows;
    }

    @Override
    public int deleteById(int id) {
        int rows = 0;
        String sql = "DELETE FROM `is_role` WHERE id=?";
        Object[] obj ={id};
        rows = FoodDao.updateByArgs(sql, obj);
        return rows;
    }

    @Override
    public int insert(int id, String roleName) {
        int rows = 0;
        String sql = "INSERT INTO `orderfood`.`is_role` (`id`, `rolename`) VALUES (?, ?)";
        Object[] obj ={id,roleName};
        rows = FoodDao.updateByArgs(sql, obj);
        return rows;
    }
}
